//
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
 // ERROR: Not supported in C#: OptionDeclaration
namespace NCS_CS
{
	internal class JF2
	{
		double[] x;
		double[] y;
		int n;

		public int first;
		public int last;
		public int peak;
		double mRand;
		double zerovalue;
        //double zerovalue1 = 0;
        //double mRand1 = 0;
        //public event myErrEventHandler myErr;
		public delegate void myErrEventHandler(int n, string d);
		public int HowmanypointPersecond()
		{
			if (n < 1) {
				return 0;
			}
			int i = 0;
			double s = 0;
			s = x[0];
			for (i = 1; i <= n - 1; i++) {
				if (x[i] - x[0] > 1) {
					return i;
				}
			}
			return 0;
		}

		public void searchpeak( int peak,ref int first,ref int last, double panju)
		{
			if (n < 8)
				return;
			int i = 0;
			peak = 0;
			for (i = 1; i <= n - 1; i++) {
				if (y[i] > y[peak]) {
					peak = i;
				}
			}
			first = 0;
			for (i = peak; i >= 0; i += -1) {
				if (System.Math.Abs(y[i] - zerovalue) < mRand) {
					first = i;
					break; // TODO: might not be correct. Was : Exit For
				}
			}
			if (n < 1)
				return;
			last = n - 1;
			for (i = peak + 1; i <= n - 1; i++) {
				if (System.Math.Abs(y[i] - zerovalue) < mRand) {
					last = i;
					break; // TODO: might not be correct. Was : Exit For
				}
				if (y[peak] - y[first] == 0) {
					break; // TODO: might not be correct. Was : Exit For
				} else {
					if ((y[i] - y[first]) / (y[peak] - y[first]) <= panju) {
						last = i;
						break; // TODO: might not be correct. Was : Exit For
					}
				}
			}
			// If first > 100 Then first = first - 100
			// If last < n - 100 Then last = last + 100
		}

//UPGRADE_NOTE: Class_Initialize was upgraded to Class_Initialize_Renamed. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
        //private void Class_Initialize_Renamed()
        //{
        //    zerovalue = 0;
        //    mRand = 3F /        Math.Pow(10, 3);
        //    //
        //}
		public JF2() : base()
		{
            zerovalue = 0;
            mRand = 0;
		}
        //public void setRand(double r)
        //{
        //    mRand = r;
        //}
		public void setArr(ref double[] x1, ref double[] Y1, int n1)
		{
			n = n1;
			x = x1;
			y = Y1;
		}
//Public Function getStartPoint() As Boolean
//Dim i As int
// first = 0
// For i = 1 To n - 1
//     If y[i] - y[first] > mRand Then
		//       ''debug.print "first", Y[i], y[first]
//       first = i - 1
//       getStartPoint = True
//       Exit For
//     End If
// Next i
//End Function
//Public Function getLastPoint() As Boolean
//Dim i As int
//'must check
// last = first
// For i = first + 1 To n - 1
//   If y[i] - y(i - 1) < 0 Then
//     If y[i] - y[first] < mRand Then
		//       ''debug.print Y[i], y[first]
		//       last = i
		//       getLastPoint = True
		//       Exit For
		//     End If
		//   End If
		// Next i
		//End Function
		//Public Function getJfValue() As Single
		//Dim i As int
		//Dim r As Single, b As Single
		// b = (y[first] + y(last)) / 2
		// r = 0
		// For i = first + 1 To last
		//   '扣除基线
		//   r = r + (y[i] - b) * (x[i] - x(i - 1)) 'time * volt
		// Next i
		// getJfValue = r
		//End Function
		//Public Function getPeakPoint1() As int
		//Dim i As int
		// peak = 0
		// For i = 1 To n - 1
		//     If y[i] > y[peak] Then
		//       peak = i
		//     End If
		// Next i
		//End Function
		//
		//Public Function getStartPoint1() As Boolean
		//Dim i As int
		// first = 0
		// For i = peak To 0 Step -1
		//     If y[i] - zerovalue < mRand Then
		//       ''debug.print "first", Y[i], y[first]
//       first = i
//       getStartPoint1 = True
//       Exit For
//     End If
// Next i
//End Function
//Public Function getLastPoint1() As Boolean
//'must check
//Dim i As int
//If n < 1 Then Exit Function
// last = n - 1
// For i = peak + 1 To n - 1
//     If y[i] - zerovalue < mRand Then
		//       ''debug.print Y[i], y[first]
		//       last = i
		//       getLastPoint1 = True
		//       Exit For
		//     End If
		// Next i
		//End Function
        //public double calcZero0()
        //{
        //    int i = 0;
        //    double zerovalue = 0;
        //    double mRand = 0;
        //    int  nRand = 0;
        //    nRand = HowmanypointPersecond() * 3;
        //    if (nRand < 10)
        //        nRand = 10;
        //    if (n < nRand)
        //        return 0;
        //    for (i = 1; i <= nRand; i++) {
        //        zerovalue = zerovalue + y[i];
        //        mRand = mRand + System.Math.Abs(y[i] - y[i - 1]);
        //    }
        //    mRand = 3 * mRand / nRand;
        //    zerovalue = zerovalue / nRand;
        //    return zerovalue;
        //}
		public void calcZeroRand()
		{
			int nRand = 0;
			nRand = HowmanypointPersecond();
			if (nRand < 1)
				nRand = 1;
			if (n < nRand)
				return;
			int i = 0;
			int            f = 0;
			int l = 0;
			int gu = 0;
			int peak = 0;
			peak = 0;
			gu = 0;
			for (i = 1; i <= n - 1; i++) {
				if (y[i] > y[peak]) {
					peak = i;
				}
			}
			for (i = 1; i <= peak; i++) {
				if (y[i] < y[gu]) {
					gu = i;
				}
			}
			f = 1;
			l = n - 1;
			if (gu - nRand > f)
				f = gu - nRand;
			if (gu + nRand < l)
				l = gu + nRand;
			//f = 1
			//l = nRand - 1
			zerovalue = 0;
			mRand = 0;
			for (i = f; i <= l; i++) {
				zerovalue = zerovalue + y[i];
				mRand = mRand + System.Math.Abs(y[i] - y[i - 1]);
			}
			mRand = 3 * mRand / (l - f + 1);
			//3倍噪声
			zerovalue = zerovalue / (l - f + 1);
		}
        //public void setZeroRand(double Z, double r)
        //{
        //    zerovalue = Z;
        //    mRand = r;
        //}
		public double getjfvalueNew(double bjsp, double panju)
		{
			int i = 0;
			//If n < 1 Then Exit Function

			calcZeroRand();
			//If bjsp <> 0 Then
			mRand = bjsp;
			//End If
			modLog.savelogmsg("zero:" + Convert.ToString(zerovalue));
			first = 0;
			last = 0;
			peak = 0;
			searchpeak( peak, ref first, ref last, panju);
			double r = 0;
			//, b As Single
			// b = (y[first] + y(last)) / 2
			if (first == 0)
				first = 1;
			//'
			r = 0;
			for (i = first; i <= last; i++) {
				//r = r + (y[i] - (y[first] + y(last)) / 2) * (x[i] - x(i - 1))
				r = r + (y[i] - y[first]) * (x[i] - x[i - 1]);
				//debug.print y[i] - zerovalue, x[i] - x(i - 1), r
			}
			return r;
		}

        //public double calcRand()
        //{
        //    int i = 0;
        //    double t = 0;
        //    for (i = 1; i <= n - 1; i++) {
        //        t = t + System.Math.Abs(y[i] - y[i - 1]);
        //    }
        //    return t / (n - 1);
        //}
        //public double calcRand1()
        //{
        //    double[] ynew = null;
        //    int ynum = 0;
        //    int i = 0;
        //    double t = 0;
        //    ynum = 1000;
        //    ynew = new double[ynum + 1];
        //    generateArr(ref ynew, ynum);
        //    for (i = 1; i <= ynum - 1; i++)
        //    {
        //        t = t + System.Math.Abs(ynew[i] - ynew[i - 1]);
        //    }
        //    return t / (ynum - 1);
        //}
        //public void generateArr(ref double[] ynew, int n)
        //{
        //    //y() 0 to ubound(y)
        //    //生成梯度数组
        //    int Count = 0;
        //    int num = 0;
        //    double Sig_Renamed = 0;
        //    bool cont = false;
        //    cont = true;
        //    Count = 0;
        //    ModMain.setDynaArr(ref ynew, Count, y[0]);
        //    do
        //    {
        //        num = num + 1;
        //        if (num > n - 1)
        //        {
        //            n = Count + 1; return;
        //        }
        //        Sig_Renamed = y[num] - y[num - 1];
        //    } while (Sig_Renamed == 0);
        //    while (cont)
        //    {
        //        while (true)
        //        {
        //            num = num + 1;
        //            if (num > n - 1)
        //            {
        //                cont = false; break; // TODO: might not be correct. Was : Exit Do
        //            }
        //            if ((y[num] - y[num - 1]) * Sig_Renamed < 0)
        //            {
        //                Count = Count + 1;
        //                ModMain.setDynaArr(ref ynew, Count, y[num - 1]);
        //                break; // TODO: might not be correct. Was : Exit Do
        //            }
        //        }
        //        Sig_Renamed = y[num] - y[Count];
        //    }
        //    n = Count + 1;
        //}
	}
}
